package com.chat.common.chat.service.adapter;

import com.chat.common.chat.domain.entity.GroupMember;
import com.chat.common.chat.domain.enums.GroupRoleEnum;
import com.chat.common.chat.domain.vo.response.ChatMemberListResp;
import com.chat.common.user.domain.entity.User;
import com.chat.common.websocket.domain.enums.WSBaseResp;
import com.chat.common.websocket.domain.enums.WSRespTypeEnum;
import com.chat.common.websocket.domain.vo.resp.ws.ChatMemberResp;
import com.chat.common.websocket.domain.vo.resp.ws.WSMemberChange;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.chat.common.websocket.domain.vo.resp.ws.WSMemberChange.CHANGE_TYPE_ADD;
import static com.chat.common.websocket.domain.vo.resp.ws.WSMemberChange.CHANGE_TYPE_REMOVE;

public class MemberAdapter {

    public static List<ChatMemberListResp> buildMemberList(List<User> memberList) {
       return memberList.stream().map(m-> ChatMemberListResp.builder()
               .avatar(m.getAvatar())
               .name(m.getName())
               .uid(m.getId())
               .build()).collect(Collectors.toList());
    }
    public static List<ChatMemberListResp> buildMemberList(Map<Long, User> batch) {
        return buildMemberList(new ArrayList<>(batch.values()));
    }

    public static WSBaseResp<WSMemberChange> buildMemberRemoveWS(Long roomId, Long uid) {
        WSBaseResp<WSMemberChange> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.MEMBER_CHANGE.getType());
        WSMemberChange wsMemberChange = new WSMemberChange();
        wsMemberChange.setUid(uid);
        wsMemberChange.setRoomId(roomId);
        wsMemberChange.setChangeType(CHANGE_TYPE_REMOVE);
        wsBaseResp.setData(wsMemberChange);
        return wsBaseResp;
    }

    public static WSBaseResp<WSMemberChange> buildMemberAddWS(Long roomId, User user) {
        WSBaseResp<WSMemberChange> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.MEMBER_CHANGE.getType());
        WSMemberChange wsMemberChange = new WSMemberChange();
        wsMemberChange.setActiveStatus(user.getActiveStatus());
        wsMemberChange.setLastOptTime(user.getLastOptTime());
        wsMemberChange.setUid(user.getId());
        wsMemberChange.setRoomId(roomId);
        wsMemberChange.setChangeType(CHANGE_TYPE_ADD);
        wsBaseResp.setData(wsMemberChange);
        return wsBaseResp;
    }

    public static List<GroupMember> buildMemberAdd(List<Long> waitAddUidList, Long GroupId) {
      return waitAddUidList.stream().map(uid->GroupMember.builder()
                .role(GroupRoleEnum.MEMBER.getType())
                .uid(uid)
                .groupId(GroupId)
                .build()).collect(Collectors.toList());
    }

    public static Collection<? extends ChatMemberResp> buildMember(List<User> list) {
        return list.stream().map(a -> {
            ChatMemberResp resp = new ChatMemberResp();
            resp.setActiveStatus(a.getActiveStatus());
            resp.setLastOptTime(a.getLastOptTime());
            resp.setUid(a.getId());
            return resp;
        }).collect(Collectors.toList());
    }
}
